{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4461f9e",
   "metadata": {},
   "source": [
    "# 百度AI开放平台\n",
    "\n",
    "# 学生实践周\n",
    "\n",
    "## 通用文字识别\n",
    "\n",
    "* 接口描述\n",
    "> 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。\n",
    "\n",
    "* 请求示例\n",
    "> 1.HTTP 方法：POST\n",
    "> 2.请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "\n",
    "* 请求参数\n",
    "> 1.URL参数-token\n",
    "> 2.图片参数-图片文件\n",
    "\n",
    "* 示例代码：\n",
    "\n",
    "> 1.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4431c765",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:24:01.734786Z",
     "start_time": "2022-04-12T12:24:01.725881Z"
    }
   },
   "outputs": [],
   "source": [
    "## 实践A-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3d3c8ac4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:27:07.085555Z",
     "start_time": "2022-04-12T12:27:06.941929Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.4a9973ae9c2a957d41b8a41232951dde.315360000.1965126425.282335-25954519', 'expires_in': 2592000, 'session_key': '9mzdAvH8XdU6OtwZ+2QPrC+2vehY3ZhQfoqs8eZXv7kphnnVQsLdncBJdf0wpkwblfEtXagssZ/kXnI3RCtWnzv5WIEi4w==', 'access_token': '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'abfe6147329237d2032454ea503478cd'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=vPWWkKM4FMQkZeMh6x9uS1VP&client_secret=CGTlqMRVQhhMURTdpMaTETvnSzc3UE9S'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d8ab997",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:33:43.409709Z",
     "start_time": "2022-04-12T12:33:42.613388Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '6'}], 'words_result_num': 1, 'log_id': 1513857889228959689}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('cmei.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcb662c2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T11:53:49.993828Z",
     "start_time": "2022-04-12T11:53:49.984418Z"
    }
   },
   "source": [
    "## 网络图片识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dbacf75a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T11:54:29.979888Z",
     "start_time": "2022-04-12T11:54:29.975187Z"
    }
   },
   "outputs": [],
   "source": [
    "## 实践A-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0f26c07b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:45:03.560178Z",
     "start_time": "2022-04-12T12:45:03.015020Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': 'WWW .ZITICHINA.COM'}, {'words': '特'}, {'words': '官方网址www.zitichina .com字体中国原创设计一一字体设计标志设计专家联系QQ:1010668565电话:028-89922910(转载请保留)'}], 'words_result_num': 3, 'log_id': 1513860742161162797}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('zhu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f106e646",
   "metadata": {},
   "source": [
    "## 手写文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d071b637",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c1ba88d7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:46:55.219659Z",
     "start_time": "2022-04-12T12:46:54.842991Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '跆拳'}, {'words': '字体/DESIGN '}, {'words': '2018'}], 'words_result_num': 3, 'log_id': 1513861210308696103}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('tai.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c943f57b",
   "metadata": {},
   "source": [
    "## 二维码识别\n",
    "草料二维码生成器https://cli.im/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5555720",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b71a5678",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:49:47.403656Z",
     "start_time": "2022-04-12T12:49:46.747707Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['玉藻'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513861931859657783}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('chong.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e29d7a05",
   "metadata": {},
   "source": [
    "## 卡片类文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c10e4614",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T11:59:05.623168Z",
     "start_time": "2022-04-12T11:59:05.614501Z"
    }
   },
   "outputs": [],
   "source": [
    "## 实际-5 身份证 & 银行卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ac46a03f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-12T12:51:41.699577Z",
     "start_time": "2022-04-12T12:51:40.552943Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 739, 'left': 1185, 'width': 299, 'height': 121}, 'words': '奥巴马'}, '民族': {'location': {'top': 954, 'left': 1737, 'width': 324, 'height': 123}, 'words': '哈尼'}, '住址': {'location': {'top': 1427, 'left': 1153, 'width': 1111, 'height': 341}, 'words': '盛顿特区宜宾法尼亚大道1600号白宫'}, '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '出生': {'location': {'top': 1185, 'left': 1168, 'width': 956, 'height': 123}, 'words': '19610804'}, '性别': {'location': {'top': 965, 'left': 1185, 'width': 108, 'height': 129}, 'words': '男'}}, 'words_result_num': 6, 'idcard_number_type': 0, 'image_status': 'unknown', 'log_id': 1513862410612110350}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('ao.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = '24.932f528cf923e3a5dac6ca6d77d086b1.2592000.1652358425.282335-25954519'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e99e6bc3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a7f39f9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f198e506",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42e2b7f7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53cee757",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "814ef508",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
